package com.example.finalwork.controller;

import com.example.finalwork.model.User;
import com.example.finalwork.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private UserService userService;

    @GetMapping
    public String registerPage(){
        return "register.html";
    }
    @PostMapping
    public ResponseEntity<String> registerUser(@RequestBody User user) {
        if (userService.findByUsername(user.getUsername()) != null) {
            return ResponseEntity.badRequest().body("用户名已存在，请选择其他用户名。");
        }
        // 保存用户到数据库
        userService.saveUser(user);
        return ResponseEntity.ok("注册成功");  // 返回注册成功的响应
    }
}
